package com.zenithmind.note.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.note.pojo.vo.NoteVO;
import com.zenithmind.note.service.NoteFavoriteService;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.zenithmind.note.pojo.entity.NoteFavorite;
import com.zenithmind.note.pojo.query.NoteFavoriteQuery;
import com.zenithmind.note.pojo.query.UserFavoriteQuery;
import java.util.List;

/**
 * 笔记收藏控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/note-favorite") 
@RequiredArgsConstructor
@Tag(name = "笔记收藏管理")
public class NoteFavoriteController {
    
    private final NoteFavoriteService noteFavoriteService;
    
    /**
     * 收藏笔记
     */
    @Log(description = "收藏笔记", operationType = OperationTypeEnum.ADD)
    @PostMapping("/favoriteNote/{noteId}")
    @Operation(summary = "收藏笔记")
    public Result<Boolean> favoriteNote(@PathVariable String noteId) {
        log.info("收藏笔记：{}", noteId);
        return noteFavoriteService.favoriteNote(noteId);
    }
    
    /**
     * 取消收藏笔记
     */
    @Log(description = "取消收藏笔记", operationType = OperationTypeEnum.DELETE)
    @DeleteMapping("/unfavoriteNote/{noteId}")
    @Operation(summary = "取消收藏笔记")
    public Result<Boolean> unfavoriteNote(@PathVariable String noteId) {
        log.info("取消收藏笔记：{}", noteId);
        return noteFavoriteService.unfavoriteNote(noteId);
    }
    
    /**
     * 查询用户收藏的笔记列表
     */
    @Log(description = "查询用户收藏的笔记列表", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getUserFavorites")
    @Operation(summary = "查询用户收藏的笔记列表")
    public Result<PageResult<NoteVO>> getUserFavorites(UserFavoriteQuery query) {
        log.info("查询用户收藏的笔记列表：{}", query);
        return Result.success(noteFavoriteService.getUserFavoritePage(query));
    }
    
    /**
     * 判断用户是否收藏了笔记
     */
    @Log(description = "判断用户是否收藏了笔记", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/checkNoteFavorite/{noteId}")
    @Operation(summary = "判断用户是否收藏了笔记")
    public Result<Boolean> isFavorited(@PathVariable String noteId) {
        log.info("判断用户是否收藏了笔记：noteId={}", noteId);
        return noteFavoriteService.isFavorited(noteId);
    }
    
    /**
     * 分页查询收藏记录（管理功能）
     */
    @Log(description = "分页查询收藏记录", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/favoriteManagePage")
    @Operation(summary = "分页查询收藏记录-管理功能")
    public Result<PageResult<NoteFavorite>> favoriteManagePage(NoteFavoriteQuery query) {
        log.info("管理端分页查询收藏记录：{}", query);
        return Result.success(noteFavoriteService.favoriteManagePage(query));
    }
    
    /**
     * 获取收藏详情（管理功能）
     */
    @Log(description = "获取收藏详情", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getFavoriteById")
    @Operation(summary = "获取收藏详情-管理功能")
    public Result<NoteFavorite> getFavoriteById(@RequestParam("favoriteId") String favoriteId) {
        log.info("管理端获取收藏详情：{}", favoriteId);
        return Result.success(noteFavoriteService.getFavoriteById(favoriteId));
    }

    /**
     * 添加或修改收藏记录（管理功能）
     */
    @Log(description = "添加或修改收藏记录", operationType = OperationTypeEnum.UPDATE)
    @PostMapping("/addOrUpdateFavorite")
    @Operation(summary = "添加或修改收藏记录-管理功能")
    public Result<NoteFavorite> addOrUpdateFavorite(@RequestBody NoteFavorite favorite) {
        log.info("添加或修改收藏记录：{}", favorite);
        return Result.success(noteFavoriteService.addOrUpdateFavorite(favorite));
    }
    
    /**
     * 批量删除收藏记录（管理功能）
     */
    @Log(description = "批量删除收藏记录", operationType = OperationTypeEnum.DELETE)
    @DeleteMapping("/deleteBatchFavorites")
    @Operation(summary = "批量删除收藏记录-管理功能")
    public Result<Boolean> deleteBatchFavorites(@RequestBody List<String> ids) {
        log.info("批量删除收藏记录：{}", ids);
        noteFavoriteService.deleteBatch(ids);
        return Result.success(true);
    }
} 